package tg.app.rj.tslx.virtual;

import java.rmi.RemoteException;
import java.util.Date;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSet;
import psdi.mbo.MboSetRemote;
import psdi.mbo.NonPersistentMbo;
import psdi.mbo.NonPersistentMboRemote;
import psdi.mbo.StatefulMbo;
import psdi.server.MXServer;
import psdi.util.MXException;

public class TslxChangeStatus extends NonPersistentMbo
  implements NonPersistentMboRemote
{
  private static final long serialVersionUID = 2423597058549413000L;
  boolean fromResult;

  public TslxChangeStatus(MboSet ms)
    throws MXException, RemoteException
  {
    super(ms);
    this.fromResult = false;
  }

  public void add() throws MXException, RemoteException {
    super.add();
    Date currentDT = MXServer.getMXServer().getDate();
    setValue("statdate", currentDT, 11L);
    StatefulMbo statusOwner = (StatefulMbo)getOwner();
    if (statusOwner.getTargetStatusOption() != null) {
      setValue("genstatusaction", false, 11L);
      String defaultstatus = getTranslator().toExternalDefaultValue("TSLXSTATUS", statusOwner.getTargetStatusOption(),statusOwner);
      String setdefaultstatus = null;
      MboSetRemote statuslist = statusOwner.getValidStatusList();
      if (!statuslist.isEmpty()) {
        setdefaultstatus = statuslist.getMbo(0).getString("value");
        int index = 0;
        MboRemote wostatuslist = statuslist.getMbo(index);

        while (wostatuslist != null)
        {
          if (wostatuslist.getString("value").equalsIgnoreCase(
            defaultstatus)) {
            setdefaultstatus = defaultstatus;
            break;
          }
          index++;
          wostatuslist = statuslist.getMbo(index);
        }
        setValue("status", setdefaultstatus, 11L);
      }
      if ((statusOwner.getOnListTab()) && (isNull("status"))) {
        statuslist = statusOwner.getStatusList();
        if (!statuslist.isEmpty()) {
          setdefaultstatus = statuslist.getMbo(0).getString("value");
          int index = 0;
          MboRemote wostatuslist = statuslist.getMbo(index);

          while (wostatuslist != null)
          {
            if (wostatuslist.getString("value").equalsIgnoreCase(
              defaultstatus)) {
              setdefaultstatus = defaultstatus;
              break;
            }
            index++;
            wostatuslist = statuslist.getMbo(index);
          }
          setValue("status", setdefaultstatus, 11L);
        }
      }
    }
  }

  public void setFromResult(boolean flag) throws MXException, RemoteException
  {
    this.fromResult = flag;
  }

  public boolean getFromResult() throws MXException, RemoteException {
    return this.fromResult;
  }
}